package client;

import exception.AddOrderException;
import exception.AuthClientException;
import exception.RemoveClientException;
import marketdepth.MarketDepth;
import order.OrderType;
import order.UnderdoneOrder;

import java.io.IOException;
import java.util.logging.Logger;

public class MessageHandler {
    private static Logger log = Logger.getLogger(MessageHandler.class.getName());

    public static void sendMessage(MessagePrefix messagePrefix,
                                   String message, Client client) {
        try {
            log.info("SendMessage: " + messagePrefix + " " + message);
            client.sendMessage(messagePrefix + " " + message);
        } catch (IOException e) {
            log.severe("Error send message because of: " + e.getMessage());
        }
    }

    public static void sendMessage(MessagePrefix messagePrefix, Client client) {
        sendMessage(messagePrefix, "", client);
    }

    public static void authClient(String message, Client client, MarketDepth marketDepth)
            throws AuthClientException {
        String[] splitCommand = message.split(" ");
        if (splitCommand.length != 2) {
            throw new AuthClientException("Command must be consist of two fields");
        }
        String command = splitCommand[0];
        String clientName = splitCommand[1];
        if (!MessagePrefix.AUTH.equalsString(command)) {
            throw new AuthClientException("First command must be authorization (AUTH)");
        }
        if (!marketDepth.addClient(clientName, client)) {
            throw new AuthClientException("Client with this name already exists in the system");
        }
        log.info("Auth client, name:" + clientName);
        client.setName(clientName);
    }

    public static void removeClient(Client client, MarketDepth marketDepth)
            throws RemoveClientException {
        if (!marketDepth.removeClient(client.getName())) {
            throw new RemoveClientException("Client cannot be remove");
        }
        log.info("Client has been removed");
    }

    public static long addOrder(String message, Client client, MarketDepth marketDepth)
            throws AddOrderException {
        String[] splitCommand = message.split(" ");
        if (splitCommand.length != 4) {
            throw new AddOrderException("Command must be consist of four fields");
        }
        String command = splitCommand[0];
        String shareName = splitCommand[1];
        double price;
        int size;
        try {
            price = Double.parseDouble(splitCommand[2]);
        }
        catch (NumberFormatException e) {
            throw new AddOrderException("Error in the third field commands. There must be a real number");
        }
        try {
            size = Integer.parseInt(splitCommand[3]);
        }
        catch (NumberFormatException e) {
            throw new AddOrderException("Error in the fourth field commands. There must be an integer number");
        }
        long orderId;
        if (MessagePrefix.BUY.equalsString(command)) {
            orderId = marketDepth.addOrderBuy(shareName, new UnderdoneOrder(
                    price, size, OrderType.BUY, client.getName()));

        } else if (MessagePrefix.SALE.equalsString(command)) {
            orderId = marketDepth.addOrderSale(shareName, new UnderdoneOrder(
                    price, size, OrderType.SALE, client.getName()));

        } else {
            throw new AddOrderException("Command must be buy or sale (" +
                    MessagePrefix.BUY + ", " + MessagePrefix.SALE + ")");
        }
        return orderId;
    }
}
